import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as menuService from '../../../services/menus';
import * as accountData from '../../../services/AccountDivi'
const daoName = "KhjlpjYpfbm";
const ButtonGroup = Button.Group;
const columns = [
    { title: '数据录入项目', width: 130, sorter: true, key: 'Name', dataIndex: "Name", dispaly: true, filter: true, canEdit: true },
    { title: '部门名称', width: 120, sorter: true, key: 'OrganName', dataIndex: "OrganName", dispaly: true, filter: true, canEdit: false },
    { title: '是否导入数据', width: 120, sorter: true, key: 'DRJG', dataIndex: "DRJG", dispaly: true, filter: true },
    { title: '导入时间', width: 150, sorter: true, key: 'DRSJ', caption: "DRSJ", render: data => datetime.format(data.DRSJ, "yyyy-MM-dd hh:mm:ss"), dispaly: true, filter: true },
];
const controls = [
    { name: 'Name', required: true, type: 'text', label: '数据录入项目', grid: 12 / 24 },
    { name: 'OrganName', required: true, type: 'text', label: '部门名称', grid: 12 / 24 },
    { name: 'DRJG', required: true, type: 'text', label: '是否导入数据', grid: 12 / 24 },
    { name: "DRSJ", label: "导入时间", type: "date", grid: 23 / 24, dataType: "date" },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class BaseKhjlDjpfQkForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        let dates = new Date()
        let month = dates.getMonth() + 1
        let NIAN = dates.getFullYear()
        let JIDU = 1
        if (month <= 3) {
            JIDU = 1
        } else if (month <= 6) {
            JIDU = 2
        } else if (month <= 9) {
            JIDU = 3
        } else {
            JIDU = 4
        }
        accountData.ExecKhjlDjpf({ NIAN, JIDU }).then(function (data) {
            if (data === "OK") {
                this.props.dispatch({
                    type: `${daoName}/fetch`,
                    payload: { daoName, page: 1, pageSize: 20 }
                });
                this.loadOptionData();
            }
        }.bind(this))
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    findData() {
        let NIAN = 2018
        let JIDU = 1
        let isOK = true
        const form = this.refs.editForm.getForm();
        form.validateFields((err, values) => {
            if (!err) {
                if (values.NIAN === undefined || values.NIAN === '') {
                    message.info('请输入年份！');
                    isOK = false;
                } else {
                    NIAN = Number(values.NIAN);
                }
                if (values.JIDU === undefined || values.JIDU === '') {
                    message.info('请选择季度！');
                    isOK = false;
                } else {
                    JIDU = Number(values.JIDU);
                }
            }
        });
        if (!isOK) {
            return
        }
        accountData.ExecKhjlDjpf({ NIAN, JIDU }).then(function (data) {
            if (data === "OK") {
                this.props.dispatch({
                    type: `${daoName}/fetch`,
                    payload: { daoName, page: 1, pageSize: 20 }
                });
                this.loadOptionData();
            }
        }.bind(this))
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes } = this.props
        const pTypes = popedomTypes[permID]
        let datas = []
        const controlsFilter = [];
        datas.push({ value: '0', label: '查询', onClick: this.findData.bind(this) })
        controlsFilter.push(
            { name: 'NIAN', required: false, type: 'text', label: '年份', grid: 4 / 24, layout: { labelSpan: 12, wrapperSpan: 12 } },
            {
                name: 'JIDU', required: false, type: 'select', label: '季度', grid: 4 / 24, layout: { labelSpan: 10, wrapperSpan: 14 },
                data: [{ value: '1', label: "第一季度" }, { value: '2', label: "第二季度" }, { value: '3', label: "第三季度" }, { value: '4', label: "第四季度" }]
            }
        )
        controlsFilter.push({ name: 'CX', required: false, type: 'button-group', data: datas, grid: 10 / 24 })
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        <EditFrom ref="editForm" daoName={daoName} controls={controlsFilter} record={record} clearItemMargin={true} />

                    </div>
                    <DataTable ref="AsseFyzjTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                </div>
            </div>
        )
    }
}

function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record
    }
}
BaseKhjlDjpfQkForm = connect(mapStateToProps)(BaseKhjlDjpfQkForm)
export default BaseKhjlDjpfQkForm;

let render = (props) => React.createElement(BaseKhjlDjpfQkForm, props);
let f = new Factory()
f.set("BaseKhjlDjpfQkForm", render)
